package www.wen.com.common.lib.base.fragment

import android.view.View
import androidx.annotation.NonNull
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import www.wen.com.common.lib.base.module.BaseAbsViewModel
import com.chad.library.adapter.base.listener.OnLoadMoreListener as ChadLoadMoreListener

/**
 * @author WEN
 * @Description:加载列表的基类
 * @date 2020//25 17:48
 */
abstract class BaseListFragment<K : ViewDataBinding, T, I> :
        BaseLoadingFragment<K>(),
        OnRefreshListener,
        OnItemClickListener,
        OnItemChildClickListener,
        OnLoadMoreListener,
        ChadLoadMoreListener {

    lateinit var mBaseAbsViewModel: BaseAbsViewModel<T, I>

    lateinit var mBaseQuickAdapter: BaseQuickAdapter<I, BaseViewHolder>

    abstract val mRecyclerView: RecyclerView

    open val mSmartRefreshLayout: SmartRefreshLayout? = null

    /**
     * SmartRefreshLoadMore上拉加载 和 AdapterLoadMore 上拉加载只能有一个为true，为什么要写2种呢，我也不知道？？？
     */
    open val mIsEnableSmartRefreshLoadMore: Boolean = false

    /**
     * 是否开启BaseQuickAdapter的上拉加载
     */
    open val mIsEnableAdapterLoadMore: Boolean = true

    /**
     * 是否添加分割线,需要重新addItemDecoration
     */
    open val isAddItemDecoration = false

    /**
     * 是否添加头部,需要重新addHeadProxy
     */
    open val isAddHead = false

    /**
     * 设为true则加载到最后一页不会有"有更多数据的提示"FootView显示
     */
    open val loadMoreEnd = false

    override fun lazyInitView() {

        mBaseAbsViewModel = viewModel as BaseAbsViewModel<T, I>
        mRecyclerView.run {
            layoutManager = provideLayoutManager()
            adapter = provideAdapter().apply {
                mBaseQuickAdapter = this
                if(mIsEnableAdapterLoadMore) {
                    loadMoreModule.setOnLoadMoreListener(this@BaseListFragment)
                    loadMoreModule.isEnableLoadMore = mIsEnableAdapterLoadMore
                }
                setOnItemChildClickListener(this@BaseListFragment)
                setOnItemClickListener(this@BaseListFragment)
            }
            if (isAddItemDecoration) {
                addItemDecoration(provideItemDecoration())
            }
            if (isAddHead) {
                addHeadProxy(mBaseQuickAdapter)
            }
        }

        //如果启用此上拉加载则初始化
        mSmartRefreshLayout?.run {
            setOnRefreshListener(this@BaseListFragment)
            if(mIsEnableSmartRefreshLoadMore){
                setEnableLoadMore(mIsEnableSmartRefreshLoadMore)
                setOnLoadMoreListener(this@BaseListFragment)
            }
        }

        mBaseAbsViewModel.uiState.observe(this) {
            if (it.showSuccess != null) {
                if(it.isRefresh){
                    mSmartRefreshLayout?.finishRefresh()
                }else{
                    if(mIsEnableSmartRefreshLoadMore){
                        mSmartRefreshLayout?.finishLoadMore()
                    }
                }

                if(mIsEnableAdapterLoadMore){
                    if(it.showEnd){
                        mBaseQuickAdapter.loadMoreModule.loadMoreEnd(loadMoreEnd)
                    }else{
                        mBaseQuickAdapter.loadMoreModule.loadMoreComplete()
                    }
                }
            }

            if (it.showError != null) {
                mBaseQuickAdapter.loadMoreModule.loadMoreFail()
            }

            if(it.isRefresh && it.showError != null){
                mSmartRefreshLayout?.finishRefresh(false)
            }
        }

    }

    /**
     * item 子view点击事件
     */
    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {}

    /**
     * item 点击事件,注意如果像ServerListFirstProvider这种可以内部实现点击事件的，那么内部的点击事件是不会生效，必须把逻辑移到下面
     */
    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {}

    /**
     * 基于SmartRefreshLayout的刷新
     */
    override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {}

    /**
     * 基于SmartRefreshLayout的上拉加载
     */
    override fun onLoadMore(@NonNull refreshLayout: RefreshLayout){}

    /**
     * 基于BaseQuickAdapter的上拉加载
     */
    override fun onLoadMore(){}

    /**
     * 可重写布局管理器
     */
    open fun provideLayoutManager(): RecyclerView.LayoutManager = LinearLayoutManager(requireActivity())

    /**
     * 可重写适配器
     */
    abstract fun provideAdapter(): BaseQuickAdapter<I, BaseViewHolder>

    /**
     * 可重写RecycleView Item装饰,需要子类设置isAddItemDecoration为true
     */
    open fun provideItemDecoration(): RecyclerView.ItemDecoration =
            DividerItemDecoration(requireActivity(), RecyclerView.VERTICAL)

    /**
     * 可重写添加头部
     */
    open fun addHeadProxy(adapter: BaseQuickAdapter<*, *>) {}

}



